local amount=tonumber(ARGV[1]);
local mode=tonumber(ARGV[2]);
if (redis.call('exists',KEYS[1])==0) then
    return nil;
end;
local store=tonumber(redis.call('get',KEYS[1]));
local count=tonumber(store-amount);
if(count<0 and store>0) then
    if (mode==0) then
        return tonumber(0);
    elseif (mode==1) then
        redis.call('decrby',KEYS[1],store);
        return tonumber(store);
    end;
elseif(count<0 and store==0) then
    return tonumber(0);
elseif(count>=0) then
    redis.call('decrby',KEYS[1],amount);
    return tonumber(amount);
end;
return tonumber(-99);
